package com.platform.config;

import com.platform.interceptor.AuthorizationInterceptor;
import com.platform.resolver.LoginUserHandlerMethodArgumentResolver;
import com.platform.service.ApiUserService;
import com.platform.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import java.util.List;

/**
 * Description: 设置自定义JWT项目的用户注解解析以及权限拦截器
 *
 * @author YangLong [410357434@163.com]
 * @version V1.0
 * @date 2020/8/26
 */
@Configuration
public class ApiWebConfig extends WebConfig {
    @Autowired
    private ApiUserService userService;
    @Autowired
    private TokenService tokenService;

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        super.addArgumentResolvers(resolvers);
        LoginUserHandlerMethodArgumentResolver resolver = new LoginUserHandlerMethodArgumentResolver();
        resolver.setUserService(userService);
        resolvers.add(resolver);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        AuthorizationInterceptor interceptor = new AuthorizationInterceptor();
        interceptor.setTokenService(tokenService);
        InterceptorRegistration registration = registry.addInterceptor(interceptor);
        registration.addPathPatterns("/api/**");
    }
}
